package com.gonsin.gnews.example.controller;

import com.gonsin.gnews.annotations.*;
import com.gonsin.gnews.annotations.model.*;
import com.gonsin.gnews.annotations.services.TemplateService;
import com.gonsin.gnews.example.mapper.SimpleMapper;
import com.gonsin.gnews.example.news.TemplateCommitPo;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping
public class TestNewsController implements InitializingBean, ApplicationContextAware {

    @Autowired private INewsService newsService;

    @Autowired private SimpleMapper mapper;


    @GetMapping("/api/test/send/news")
    public String testSend(){
        mapper.select();

        // 构建自己的消息内容
        TemplateCommitPo commitPo = new TemplateCommitPo();
        commitPo.setConferenceContent("此处为会议内容");
        commitPo.setName("此处为会议名字");
        commitPo.setPlace("公信大楼10楼大会议室");
        commitPo.setStartTime(new TimePo(System.currentTimeMillis()));
        // ...其他省略

        newsService.sendNews("GONSIN", commitPo, "13434929333");
        return "ok";
    }



    @Override
    public void afterPropertiesSet() throws Exception {
        // TODO 测试用代码，此处添加一个测试用的模板，实际应用中应该由客户在网页上自行添加模板
        TemplateService newsTemplateService = context.getBean(TemplateService.class);
        GNewsPageRequest request = new GNewsPageRequest();
        request.setPage(0);
        request.setCount(1);
        GNewsPageResult<NewsTemplateBean> templates = newsTemplateService.searchTemplates("GONSIN", new NewsTemplateSearchPo(), request);
        if(CollectionUtils.isEmpty(templates.getData())){
            // 添加一个新的模板
            NewsTemplatePo template = new NewsTemplatePo();
            template.setContent("公信会议中心诚邀您参加${name}会议，会议内容为【${conferenceContent}】，请于【${startTime.MM}月${startTime.dd}日】在${place}参与会议。");
            template.setName("会议预约通知");
            template.setPlatform("sms");
            newsTemplateService.addNewsTemplate("GONSIN", "TemplateCommitPo", template);
        }



        // 修改一个模板
        {
            NewsTemplatePo template = new NewsTemplatePo();
            template.setContent("公信会议中心诚邀您参加${name}会议，会议内容为【${conferenceContent}】，请于【${startTime.MM}月${startTime.dd}日】在${place}参与会议。");
            template.setName("会议预约通知");
            template.setPlatform("sms");
            GNewsPageRequest page = new GNewsPageRequest();
            page.setPage(0);
            page.setCount(1);
            NewsTemplateBean bean = newsTemplateService.searchTemplates("GONSIN", new NewsTemplateSearchPo(), page).getData().get(0);
            newsTemplateService.updateNewsTemplate(bean.getNewsTemplateKey(), template);
        }

    }

    private ApplicationContext context;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.context = applicationContext;
    }
}
